(function (w) {
  var login = {
    init: function () {
      var self = this
      self.event()
    },
    event: function () {
      var self = this
      $('.js-login-btn').click(function () {
        self.login()
      })
      $('.js-psd').keydown(function (e) {
        if (e.keyCode == 13)self.login()
      })
      // 浏览器下载
      $('.download-browser').hover(
        function(){
          $('.browser-backdrop').show()
        },
        function(){
          $('.browser-backdrop').hide()
        }
      )
    },
    // 登录
    login: function () {
      var self = this,
        md_passwd = null;

      if (self.validate.account($('.js-account').val().trim()) && self.validate.psd($('.js-psd').val().trim()) && self.validate.companyCode($('.js-company-code').val().trim())) {
        md_passwd = hlBase64Util.encode(hlBase64Util.encode($('.js-psd').val().trim()))

        hlhttp.post({
          url: 'user/login',
          data: {
            passwd: md_passwd,
            account: $('.js-account').val().trim(),
            tenant_code: $('.js-company-code').val().trim(),
          },
          success: function (data) {
            var userMes = {
              id: data.id,
              name: data.name,
              account: data.account,
              phone: data.phone,
              email: data.email,
              tenantType: data.tenantType,
              tenantId:data.tenantId,
              isAdmin:data.isAdmin,
              ico: data.cstmFaviconUrl || '',
              logo: data.cstmLogoUrl || '',
              title: data.cstmWebTitle || '',
              preSysCode: data.preSysCode || '',
              cstmHomepage: data.cstmHomepage || '',
              oauthType:data.oauthType || ''
            }

            Cookies.set("token", data.token, {expires: 30})
            window.localStorage.setItem('userMes', JSON.stringify(userMes))
            window.sessionStorage.setItem('names', data.name)
            // 清除原来所用的存储字段
            var originalArr = [
              'names',
              'navPos',
              'projectName',
              'unitData',
              'contractNav1'
            ]
            hlStorage.lsClearMultipleKey(originalArr)

            var pathname = window.location.pathname.replace('login', 'index')
            window.location.href = './#/home';
          }
        })
      }

    },
    // 验证
    validate: {
      account: function (str) {
        if (!str) {
          hl.toast('帐号不能为空')
          return false
        }
        return true
      },
      psd: function (str) {
        if (str.length < 6) {
          hl.toast('登录密码至少需要6位字符')
          return false
        }
        return true
      },
      companyCode: function (str) {
        if (!str) {
          hl.toast('企业代码不能为空')
          return false
        }
        return true
      }
    }
  }

  login.init()
}(window))
